package jmars.mobile.rbscluster;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class GenArff {

	private static String skipString ="%";
	
	private static String module="yuzhong";
	private static String filename="rbs_cluster";
	
	private static String path="E:\\BaiduYunDownload\\afterprocess\\";
	private static String out=path+module+"\\"+filename;
	private static String inUserEntropy=path+module+"\\EntropyTrafficTotal_LAC_CI-phone_number.txt";
	private static String inHourlyTraffic=path+module+"\\EntropyTrafficTotalHourly_LAC_CI.txt";
	private static String inTypeEntropy=path+module+"\\EntropyTrafficTotal_LAC_CI-traffic_type.txt";
	private static String lookup=path+module+"\\lac_ci_geolocation.txt";
	
	private static Map<String,String> userMap = new HashMap<String,String>();
	private static Map<String,String> typeMap = new HashMap<String,String>();
	
	private static Map<String,String> lookupMap = new HashMap<String,String>();
	
	
	private static boolean filter(String id){
		if(!lookupMap.containsKey(id))return false;
		double minLong=106.481719;
		double minLat=29.526970;
		double maxLong=106.588321;
		double maxLat=29.571471;
		String[] cols=lookupMap.get(id).split(",");
		double lat=Double.parseDouble(cols[3]);
		double lon=Double.parseDouble(cols[4]);
		if(lon<minLong || lon> maxLong || lat<minLat || lat>maxLat)
			return false;
		return true;
	}
	
	public static void main(String[] args) throws IOException{
		String row;
		String[] cols;
		FileWriter resultWriter;
		resultWriter = new FileWriter(out);
		File flookup = new File(lookup);
		FileInputStream fisLookup = new FileInputStream(flookup);
		BufferedReader readerLookup = new BufferedReader(new InputStreamReader(fisLookup));
		while((row=readerLookup.readLine())!=null){
			if(row.startsWith("%"))continue;
			cols=row.split(",");
			lookupMap.put(cols[0], row);
		}
		fisLookup.close();
		System.out.println("lookup set: "+lookupMap.size());
		File fuser = new File(inUserEntropy);
		FileInputStream fisUser = new FileInputStream(fuser);
		BufferedReader readerUser = new BufferedReader(new InputStreamReader(fisUser));
		while((row=readerUser.readLine())!=null){
			if(row.startsWith(skipString))continue;
			cols=row.split(",");
			userMap.put(cols[0],cols[1]);
		}
		File ftype = new File(inTypeEntropy);
		FileInputStream fisType = new FileInputStream(ftype);
		BufferedReader readerType = new BufferedReader(new InputStreamReader(fisType));
		while((row=readerType.readLine())!=null){
			if(row.startsWith(skipString))continue;
			cols=row.split(",");
			typeMap.put(cols[0],cols[1]);
		}
		
		File fileHourly = new File(inHourlyTraffic);
		FileInputStream fisHourly = new FileInputStream(fileHourly);
		BufferedReader readerHourly = new BufferedReader(new InputStreamReader(fisHourly));
		/*
		resultWriter.write("@RELATION rbs\r\n"
				+ "@ATTRIBUTE id STRING\r\n"
				+ "@ATTRIBUTE hourEntropy NUMERIC\r\n"
				+ "@ATTRIBUTE traffic1 NUMERIC\r\n"
				+ "@ATTRIBUTE traffic2 NUMERIC\r\n"
				+ "@ATTRIBUTE traffic3 NUMERIC\r\n"
				+ "@ATTRIBUTE traffic4 NUMERIC\r\n"
				+ "@ATTRIBUTE traffic5 NUMERIC\r\n"
				+ "@ATTRIBUTE traffic6 NUMERIC\r\n"
				+ "@ATTRIBUTE traffic7 NUMERIC\r\n"
				+ "@ATTRIBUTE traffic8 NUMERIC\r\n"
				+ "@ATTRIBUTE traffic9 NUMERIC\r\n"
				+ "@ATTRIBUTE traffic10 NUMERIC\r\n"
				+ "@ATTRIBUTE traffic11 NUMERIC\r\n"
				+ "@ATTRIBUTE traffic12 NUMERIC\r\n"
				+ "@ATTRIBUTE traffic13 NUMERIC\r\n"
				+ "@ATTRIBUTE traffic14 NUMERIC\r\n"
				+ "@ATTRIBUTE traffic15 NUMERIC\r\n"
				+ "@ATTRIBUTE traffic16 NUMERIC\r\n"
				+ "@ATTRIBUTE traffic17 NUMERIC\r\n"
				+ "@ATTRIBUTE traffic18 NUMERIC\r\n"
				+ "@ATTRIBUTE traffic19 NUMERIC\r\n"
				+ "@ATTRIBUTE traffic20 NUMERIC\r\n"
				+ "@ATTRIBUTE traffic21 NUMERIC\r\n"
				+ "@ATTRIBUTE traffic22 NUMERIC\r\n"
				+ "@ATTRIBUTE traffic23 NUMERIC\r\n"
				+ "@ATTRIBUTE traffic24 NUMERIC\r\n"
				+ "@ATTRIBUTE userEntropy NUMERIC\r\n"
				+ "@ATTRIBUTE typeEntropy NUMERIC\r\n"
				+ "@ATTRIBUTE trafficSum NUMERIC\r\n");
				*/
		Long trafficSum;
//		Long trafficVar;
		Long maxTraffic=0L;
		Long minTraffic=null;
		int rowcount=0;
		Long temp; 
		main:while((row=readerHourly.readLine())!=null){
			trafficSum=0L;
//			trafficVar=0L;
			if(row.startsWith(skipString))continue;
			cols=row.split(",");
			if(!filter(cols[0]))continue;
			if(userMap.containsKey(cols[0])&&typeMap.containsKey(cols[0])){
				for(int i=1;i<=24;i++){
					temp=Long.parseLong(cols[i+1]);
					if(temp<1000) continue main;
					trafficSum+=temp;
				}
				if(trafficSum>3E9) continue main;
				rowcount++;
//				for(int i=1;i<=24;i++){
//					trafficVar +=(trafficSum/24-Long.parseLong(cols[i+1]))*(trafficSum/24-Long.parseLong(cols[i+1]));
//				}
				if(maxTraffic<trafficSum)maxTraffic=trafficSum;
				if(minTraffic==null||minTraffic>trafficSum)minTraffic=trafficSum;
				resultWriter.write(row+","+ userMap.get(cols[0])
						+","+typeMap.get(cols[0])+","+trafficSum+"\r\n");
			}
		}
		System.out.println("maxTraffic: "+maxTraffic);
		System.out.println("minTraffic: "+minTraffic);
		
		fisUser.close();
		fisType.close();
		fisHourly.close();
		resultWriter.close();
		File file=new File(out);  
		if(file.exists()){
			file.renameTo(new File(out+rowcount));
		}
		System.out.println("finished: "+out+rowcount);
	}
	

}
